<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 1] 1.2 A Virtual Machine</TITLE>
<META NAME="author" CONTENT="Pat Niemeyer and Josh Peck">
<META NAME="date" CONTENT="Tue Jul 22 18:47:16 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Exploring Java">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Exploring Java" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch01_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 1<br>Yet Another Language?</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch01_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="EXJ-CH-1-SECT-2">1.2 A Virtual Machine</A></h2>

<P CLASS=para>
<A NAME="CH01.VIRTUALMACHINE"></A>Java is both a compiled and an interpreted language. Java source code
is turned into simple binary instructions, much like ordinary
microprocessor machine code. However, whereas C or C++ source is
refined to native instructions for a particular model of processor,
Java source is compiled into a universal format--instructions for
a virtual machine.

<P CLASS=para>
Compiled Java byte-code, also called J-code, is executed by a
Java run-time interpreter. The run-time system performs all the
normal activities of a real processor, but it does so in a safe, virtual
environment. It executes the stack-based instruction set and manages a
storage heap. It creates and manipulates primitive data types, and
loads and invokes newly referenced blocks of code. Most importantly,
it does all this in accordance with a strictly defined
open specification that can be implemented by anyone who wants to
produce a Java-compliant virtual machine. Together, the virtual
machine and language definition provide a complete specification.
There are no features of Java left undefined or
implementation dependent. For example, Java specifies the sizes of all
its primitive data types, rather than leave it up to each
implementation.

<P CLASS=para>
The Java interpreter is relatively lightweight and small; it
can be implemented in whatever form is desirable for a particular
platform. On most systems, the interpreter is written in a fast,
natively compiled language like C or C++. The interpreter can be run
as a separate application, or it can be embedded in another piece of
software, such as a Web browser.

<P CLASS=para>
All of this means that Java code is implicitly portable. The
same Java application can run on any platform that provides a Java
run-time environment, as shown in <A HREF="ch01_02.htm#EXJ-CH-1-FIG-1">Figure 1.1</A>. You
don't have to produce alternate versions of your application for
different platforms, and you never have to distribute source code to
end users.

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="EXJ-CH-1-FIG-1">Figure 1.1: Java virtual machine environment</A></h4>


<p>
<img align=middle src="./figs/je0101.gif" alt="[Graphic: Figure 1-1]" width=503 height=353 border=0>

</DIV>

<P CLASS=para>
The fundamental unit of Java code is the
<I CLASS=emphasis>class</I>.  As in other object-oriented languages,
classes are application components that hold executable code and
data. Compiled Java classes are distributed in a universal binary
format that contains Java byte-code and other class
information. Classes can be maintained discretely and stored in files
or archives on a local system or on a network server. Classes are
located and loaded dynamically at run-time, as they are needed by an
application.

<P CLASS=para>
In addition to the platform-specific run-time system, Java has
a number of fundamental classes that contain
architecture-dependent methods. These <I CLASS=emphasis>native
methods</I> serve as Java's gateway to the real
world. These methods are implemented in a native language on the host
platform. They provide access to resources such as the network, the
windowing system, and the host filesystem. The rest of Java is written
entirely in Java, and is therefore portable. This includes fundamental
Java utilities like the Java compiler, which is also a Java
application and is therefore immediately available on all Java
platforms.

<P CLASS=para>
In general, interpreters are slow, but because the Java
interpreter runs compiled byte-code, Java is a fast interpreted
language. Java has also been designed so that software implementations
of the run-time system can optimize their performance by compiling
byte-code to native machine code on the fly. This is called
"just in time" compilation. Sun claims that with
just in time compilation, Java code can execute nearly
as fast as native compiled code and maintain its
transportability and security. The one performance hit that natively
compiled Java code will always suffer is array bounds checking. But on
the other hand, some of the basic design features of Java place more
information in the hands of the compiler, which allows for certain
kinds of optimizations not possible in C or C++.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch01_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch01_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Enter Java</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Java Compared</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
